/*
 * Ext JS Library 2.0 Beta 1
 * Copyright(c) 2006-2007, Ext JS, LLC.
 * licensing@extjs.com
 * 
 * http://extjs.com/license
 */
Ext.onReady(function(){
    // shorthand
 				/**
	 * 右键菜单
	 */
  var ctxMenu = new Ext.menu.Menu({
        id:'copyCtxs',
        items: [{
                id:'orgadd',
                handler:orgadd,
                cls:'expand-all',
                text:'增加'
            },{
                id:'orgmodi',
                handler:orgmodi,
                cls:'collapse-all',
                text:'修改'
            }
            ,{
                id:'orgdel',
                handler:orgdel,
                cls:'collapse-all',
                text:'删除'
            }]
    });
    
    function orgadd(){ alert("新增");}
    function orgmodi(){ alert("修改");}
    function orgdel(){ alert("删除");}
    function xx(node){ alert("xx");}
    
    var Tree = Ext.tree;
   
    var tree = new Tree.TreePanel({
        el:'tree-div',
        autoScroll:true,
        animate:true,
        ddScroll:true,
        animate:true,
        titleCollapse:true,
        loader: new Tree.TreeLoader({
            dataUrl:'textS',
            baseParams:{id:1}
        })
    });

	tree.on("beforeload", function(treeLoader, node) {
	 tree.loader.baseParams.id = treeLoader.id;
    }, this);
    // set the root node
    var root = new Tree.AsyncTreeNode({
        text: 'Ext JS',
        draggable:false,
        expandable: true,
        id:'1'
    });
    tree.setRootNode(root);

    // render the tree
    tree.render();
    root.expand();
    
    	var m1 = new Ext.tree.TreeNode({
        text: "dddfdfefe",
        draggable:false,
        id:0
    	});
    	
    	var m2 = new Ext.tree.TreeNode({
        text: "22222222222",
        draggable:false,
        singleClickExpand:true,
        id:0
    	});
    	
    	var m3 = new Ext.tree.TreeNode({
        text: "dddfdfefe",
        draggable:false,
        id:0
    	});
    	
    	
    	var mm2 = new Ext.tree.TreeNode({
        text: "dddfdfefe",
        draggable:false,
        id:0
    	});
    	
    	var mm3 = new Ext.tree.TreeNode({
        text: "dddfdfefe",
        draggable:false,
        id:0
    	});
    	
    	m2.appendChild(mm2);
    	m2.appendChild(mm3);
    	
    	root.appendChild(m1);
    	root.appendChild(m2);
    	root.appendChild(m3);

    tree.on('contextmenu',prepareCtx);
   
     tree.on('click', function(node){
     if(node.isLeaf())
     {
         alert("dddd");
      }
       else{ alert("dd");};
	});

   function prepareCtx(node, e){
        node.select();
	    ctxMenu.showAt(e.getXY());
    }
});
 